{extend name="public:base" /}

{block name="body"}


<div class="main" style="padding:15px;">
    <div class="content">
        <span style="margin-left: 0;display: inline-block">购物车列表</span>
        <span class="userinfo" id="total">总价格:0</span>
        <div></div>
    </div>

    <table class="layui-table">
        <thead>
        <tr>
            <th style="text-align: center;font-weight: bold">ID</th>
            <th style="text-align: center;font-weight: bold">商品标题</th>
            <th style="text-align: center;font-weight: bold">分类</th>
            <th style="text-align: center;font-weight: bold">原价</th>
            <th style="text-align: center;font-weight: bold">折扣</th>
            <th style="text-align: center;font-weight: bold">现价</th>
            <th style="text-align: center;font-weight: bold">数量</th>
            <th style="text-align: center;font-weight: bold">合计</th>
            <th style="text-align: center;font-weight: bold">操作</th>
        </tr>
        </thead>
        <tbody>
        {volist name="right" id='right'}
        <tr>
            <td style="text-align: center">{$right.goods_id}</td>
            <td style="text-align: center">{$right.title}</td>
            <td style="text-align: center">{$right.cat|getCatName}</td>
            <td style="text-align: center">{$right.price}</td>
            <td style="text-align: center">{$right.discount}</td>
            <td style="text-align: center">
                {if $right.discount <> 0}
                {$price = $right.discount*0.1*$right.price}
                {else/}{$price = $right.price}
                {/if}
            </td>
            <td style="text-align: center">{$right.quantity}</td>
            <td style="text-align: center">{$right.quantity*$price}</td>

            <td style="text-align: center" >
                <button class="layui-btn layui-btn-xs" id="cart" onclick="pay({$right.goods_id},{$price*$right.quantity},'{$right.title}',{$right.quantity})">立即支付</button>
                <button class="layui-btn layui-btn-xs" id="delete" onclick="delOne({$right.goods_id},'{$right.title}',{$right.quantity})">移除购物车</button>
            </td>
        </tr>
        <input type="hidden" value="
            {if $right.discount <> 0}
            {$count = $count + $right.discount*0.1*$right.price*$right.quantity}
            {else/}
            {$count = $count + $right.price*$right.quantity}
            {/if}"
        >
        {/volist}
        </tbody>
    </table>
    <div style="align-self: center; margin: 0 auto;">
        <input type="hidden" value="{$count}" id="cou">
        <button class="layui-btn layui-btn-ms center-block" id="cart1" onclick="buyAll()">一键购买所有</button>
    </div>

    <div style="text-align: center">{$rightList|raw}</div>

</div>

<script>
    window.onload=function () {
        var count=document.getElementById('cou');
        $('#total').text('当前页面总价格: '+count.value);
    }
</script>

<script>
    function delOne(id,title,quantity){
        layer.confirm('确定要移除 "'+title+' "吗?', {
            icon:3,
            btn: ['确定','取消']
        }, function(){
            $.post('/index.php/user/delOneOnCart',{'goods_id':id,'quantity':quantity},function(res){
                if(res.status == 0){
                    layer.alert(res.message,{icon:2});
                }else{
                    layer.msg(res.message);
                    setTimeout(function () {
                        window.location.reload();
                    },1100);
                }
            },'json');
        });
    }
</script>

<script>
    function pay(id,price,title,quantity){
        layer.confirm('确定购买这么多吗？你很有钱吗？', {
            icon:3,
            btn: ['确定','取消']
        }, function(){
            $.post('/index.php/user/buyOne',{'goods_id':id,'quantity':quantity,'total_money':price},function(res){
                if(res.status == 0){
                    layer.alert(res.message,{icon:2});
                }else{
                    layer.msg('钱包不足，强制进行本次交易');
                    setTimeout(function () {
                        window.location.reload();
                    },1800);
                }
            },'json');
        });
    }
</script>

<script>
    function buyAll(){
        layer.confirm('确定购买这么多吗？你很有钱吗？', {
            icon:3,
            btn: ['确定','取消']
        }, function(){
            $.post('/index.php/user/buyAll',function(res){
                if(res.status == 0){
                    layer.alert(res.message,{icon:2});
                }else{
                    layer.msg('购买成功');
                    setTimeout(function () {
                        window.location.reload();
                    },1800);
                }
            },'json');
        });
    }

</script>

{/block}

